package com.newvpn.fastbest.zenith.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.newvpn.fastbest.zenith.R;
import com.newvpn.fastbest.zenith.VPNApp;
import com.newvpn.fastbest.zenith.dialog.TestNetReultDialog;
import com.newvpn.fastbest.zenith.presenter.AdPresenter;
import com.newvpn.fastbest.zenith.utils.DeviceUtils;
import com.newvpn.fastbest.zenith.utils.VLog;

import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class LanchLayout {

    Disposable countDownDisposable;

    MainActivity activity;

    private View lanchView;


    public LanchLayout(MainActivity activity) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_lauch);
        this.activity = activity;
        lanchView = activity.findViewById(R.id.lanchView);
        ProgressBar process = activity.findViewById(R.id.process);
        TextView  tvProcess = activity.findViewById(R.id.tvProcess);
        AdPresenter.INSTANCE.preSplashAd(null);
        startCountDown(process,tvProcess);
        //获取用户DeviceID
        //checkDeviceID();
    }

    public void show() {
        VLog.event("Splash_Show");
        Log.e("initLanch", "show()");
        lanchView.setVisibility(View.VISIBLE);
    }

    public void checkDeviceID() {
        String deviceID = DeviceUtils.getDeviceId();
        if (!TextUtils.isEmpty(deviceID)) {
            if (mEndCallback != null) {
                mEndCallback.endGetDevieID();
            }
            return;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                DeviceUtils.makeAdid();
            }
        }).start();
        startProcess();
    }

    private void startProcess() {
        long startTime = System.currentTimeMillis();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (true) {
                        Thread.sleep(100);
                        if (!DeviceUtils.adidIsEmpty() || System.currentTimeMillis() - startTime > 3 * 1000) {
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (mEndCallback != null) {
                                        mEndCallback.endGetDevieID();
                                    }
                                }
                            });
                            DeviceUtils.checkAdid();
                            return;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    private void startCountDown(ProgressBar button, TextView tvProcess) {
        Log.e("initLanch", "startCountDown");
        countDownDisposable = Observable.interval(0, 100, TimeUnit.MILLISECONDS) // 每0.01秒发射一次
                .map(t -> (int) (t * 100 / 30)) //
                .take(300)
                .subscribeOn(Schedulers.computation()) // 在计算线程执行
                .observeOn(AndroidSchedulers.mainThread()) // 在同一个线程上观察
                .subscribe(
                        progress -> {
                            if (progress < 97) {
//                                button.setText((3 + progress) + "%");
                                button.setProgress(3 + progress);
                                tvProcess.setText(VPNApp.getBase().getString(R.string.loading)+progress+"%");
                            } else {
//                                Intent intent = new Intent(LanchLayout.this, MainActivity.class);
//                                LanchLayout.this.startActivity(intent);
//                                finish();
                                Log.e("initLanch", "lanchView.setVisibility(View.GONE)");
                                lanchView.setVisibility(View.GONE);
                                if (mEndCallback != null) {
                                    mEndCallback.end();
                                }
                                countDownDisposable.dispose();

                            }
                        },
                        Throwable::printStackTrace,
                        () -> {
                            countDownDisposable.dispose();
                        }
                );
    }

    private EndCallback mEndCallback;

    public void setEndCallback(EndCallback callback) {
        mEndCallback = callback;
    }

    public interface EndCallback {
        void end();

        void endGetDevieID();
    }
}
